﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace SchoolManagement.AutoEvaluacion
{
    public partial class Autoevaluacion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UsuarioEval"] == null)
                Response.Redirect("~/Forms/Login.aspx", false);
        }

        /// <summary>
        /// Guarda la información
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonEnviar_Click(object sender, EventArgs e)
        {
            if (Session["UsuarioEval"] == null)
                Response.Redirect("~/Forms/Login.aspx", false);
            BussinessLogic.UsuarioEval user = Session["UsuarioEval"] as BussinessLogic.UsuarioEval;
            if (user.Login == null || user.Login.Length == 0)
                Response.Redirect("~/Forms/Login.aspx", false);
            BussinessLogic.Evaluacion miEval = new BussinessLogic.Evaluacion();

            for (int indexGrid = 0; indexGrid < GridViewCriterios.Rows.Count; indexGrid++)
            {
                GridViewRow filaActual = GridViewCriterios.Rows[indexGrid];
                int idElemento = int.Parse(GridViewCriterios.DataKeys[indexGrid].Value.ToString());
                CheckBox chkSeleccion = filaActual.Cells[2].Controls[1] as CheckBox;
                miEval.InsertUpdateCriteriosIntegracion(user.Id, idElemento, chkSeleccion.Checked);
            }


            System.Collections.Generic.List<GridView> grillas =
                new System.Collections.Generic.List<GridView>();


            grillas = Utilities.Utilities.FindControl<GridView>(Page);

            // de todas las grillas no se toma la que dice criterios
            GridView grillaEspecial = grillas.Find(x => x.ID == "GridViewCriterios");
            if (grillaEspecial != null)
                grillas.Remove(grillaEspecial);

            foreach (GridView grilla in grillas)
            {
                for (int indexGrid = 0; indexGrid < grilla.Rows.Count; indexGrid++)
                {
                    GridViewRow filaActual = grilla.Rows[indexGrid];
                    int idPregunta = int.Parse(grilla.DataKeys[indexGrid].Value.ToString());
                    RadioButton chkSeleccion1 = filaActual.Cells[3].Controls[1] as RadioButton;
                    RadioButton chkSeleccion2 = filaActual.Cells[4].Controls[1] as RadioButton;
                    RadioButton chkSeleccion3 = filaActual.Cells[5].Controls[1] as RadioButton;
                    RadioButton chkSeleccion4 = filaActual.Cells[6].Controls[1] as RadioButton;
                    RadioButton chkSeleccion5 = filaActual.Cells[7].Controls[1] as RadioButton;

                    int nivel = 1;
                    if (chkSeleccion1.Checked) nivel = 1;
                    else if (chkSeleccion2.Checked) nivel = 2;
                    else if (chkSeleccion3.Checked) nivel = 3;
                    else if (chkSeleccion4.Checked) nivel = 4;
                    else if (chkSeleccion5.Checked) nivel = 5;
                    else break;



                    miEval.InsertarUpdateEvaluacion(user.Id, idPregunta, nivel);
                }
            }

            // Agrega las observaciones y otros datos
            miEval.InsertarOtrasObservaciones(user.Id, TextBoxNivelSatisfaccion.Text, TextBoxOtros.Text);

        }
    }
}
